package com.nextel.project.cobranza.rms;
import com.nextel.project.cobranza.bean.CobranzaBean;
import pe.com.nextel.rms.GestorRMS;
import pe.com.nextel.rms.Persistente;
import pe.com.nextel.rms.RMSIndice;
import javax.microedition.rms.RecordStoreException;
import pe.com.nextel.util.ArrayList;
import pe.com.nextel.util.Iterator;
import pe.com.nextel.util.List;
import pe.com.nextel.util.math.MathUtil;

/**
 *
 * @author eespinoza
 */
public class RMSCobranza extends GestorRMS {
    
    private CobranzaBean bean = new CobranzaBean();
    private RMSIndice indRMSCobranza ;
    
    
    public RMSCobranza() {
        super("RMS_COBRANZA");
        indRMSCobranza = new RMSIndice("IDX_COBRANZA");
    }
    
    protected Persistente createObjectBean() {
        return bean;
    }

    /**
     * Obtiene la lista por cliente
     *
     */
    public List getListaCobranzaPorCliente(String codigoGrupo) throws Exception{

//        System.out.println("RMSCobranza.getListaCobranzaPorCliente");
        
        try {
            abrirRS();
            indRMSCobranza.abrirIndice();
        
//            System.out.println("codigoCliente =" + codigoGrupo + "|");
           
            int indice = indRMSCobranza.getIndiceBinario(codigoGrupo);
//            int indice =0;
//            System.out.println("-->Indice= " + indice );
            Iterator itx = null;
            List lista = new ArrayList( );
            
            if(indice > 0){
                itx = getRecordList(indice).iterator();
                
                while(itx.hasNext()){
                    CobranzaBean beanTp = (CobranzaBean)itx.next();
                    //verificamos que tenga saldo
//                    System.out.println("beanTp.getSaldo()" + beanTp.getSaldo());
                    if (MathUtil.esMayorIgual("0", beanTp.getSaldo())) {
                        continue;
                    } 
                    
                    beanTp.setIdRecodset(indice);
                    lista.add(beanTp);
                }
            }
            return lista;
            
        } finally {
            cerrarRS();
            indRMSCobranza.cerrarIndice();
        }
    }
    
    public void eliminar() throws RecordStoreException{
        deleteAll();
        indRMSCobranza.deleteAll();
    }    
}
